package com.lxj.fast_android_demo.dto;

import java.util.HashMap;

import com.lxj.fast_android_demo.util.DatasetUtil;

/**
 * 行记录对象
 * 
 * @author LiangXiangJu
 * @creationTime 2014-6-30 上午11:00:36
 * @version
 */
public class DataRecord {
	// 所属数据集
	private Dataset dataset;
	// 行记录数据map
	private HashMap<String, DataCell> datas;

	public DataRecord(Dataset dataset, HashMap<String, String> datas) {
		this.dataset = dataset;
		this.datas = new HashMap<String, DataCell>();
		// record的每列初始赋值
		for (String key : dataset.getFields().keySet()) {
			DataField dataField = dataset.getFields().get(key);
			DataCell dataCell = new DataCell(dataField, this);
			dataCell.setValue(dataField.getDefaultValue());
			this.datas.put(key, dataCell);

			if (null != datas && datas.containsKey(key)) {
				setValueForField(datas.get(key), key);
			}
		}
	}

	/**
	 * 根据属性名称获取对应值
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午11:02:41
	 * @version
	 * @param fieldName
	 * @return
	 */
	public Object getData(String fieldName) {
		if (null != fieldName && null != datas && datas.containsKey(fieldName)) {
			return datas.get(fieldName).getValue();
		}
		return null;
	}

	/**
	 * 根据属性名称获取对应值文本
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午11:03:21
	 * @version
	 * @param fieldName
	 * @return
	 */
	public String getValue(String fieldName) {
		Object result = getData(fieldName);
		if (null != result) {
			return String.valueOf(result);
		}
		return null;
	}

	/**
	 * 根据属性获取单元格对象
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-7-2 下午2:04:05
	 * @version
	 * @param fieldName
	 * @return
	 */
	public DataCell getCell(String fieldName) {
		if (null != datas && datas.containsKey(fieldName)) {
			return datas.get(fieldName);
		}
		return null;
	}

	/**
	 * 为对应属性赋值
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午11:03:45
	 * @version
	 * @param value
	 * @param fieldName
	 */
	public void setValueForField(String value, String fieldName) {
		if (null != fieldName && null != datas && datas.containsKey(fieldName)) {
			DataField currentDataField = dataset.getField(fieldName);
			if (null == currentDataField) {
				return;
			}
			Object obj = null;
			if (!DatasetUtil.isStrEmpty(value)) {
				switch (currentDataField.getType()) {
				case STRING:
					obj = value;
					break;
				case INT:
					obj = DatasetUtil.forceParseInt(value);
					break;
				case DOUBLE:
					obj = DatasetUtil.parseDouble(value);
					break;
				default:
					break;
				}
			} else {
				// 传入空值时，赋默认值
				obj = currentDataField.getDefaultValue();
			}
			setDataForField(obj, fieldName);
		}
	}

	/**
	 * 为对应属性赋值
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午11:03:53
	 * @version
	 * @param data
	 * @param fieldName
	 */
	public void setDataForField(Object data, String fieldName) {
		if (null != fieldName && null != datas && datas.containsKey(fieldName)) {
			DataCell dataCell = datas.get(fieldName);
			if (null != dataCell) {
				dataCell.setValue(data);
			} else {
				DataField currentDataField = dataset.getField(fieldName);
				dataCell = new DataCell(currentDataField, this);
				dataCell.setValue(data);
				datas.put(fieldName, dataCell);
			}
		}
	}

	public Dataset getDataset() {
		return dataset;
	}

	public HashMap<String, DataCell> getDatas() {
		return datas;
	}

}
